**------------------------------------------
** Replication materials: "Elite Cues and Economic Policy Attitudes: The Mediating Role of Economic Hardship" by Charlotte Cavaille and Anja Neundorf

** Data preparation and recoding of variables of BHPS working file

clear all
capture log close
set more off
cd "/Users/aneundorf/Dropbox/Project_CavailleNeundorf/Paper1/Analysis" // CHANGE DIRECTORY HERE


**-----------------------------------------------
** RECODE RAW BHPS DATA
use Data/BHPS_working, clear


* ========================================
* = Sample used: region and BHPS samples  =
* ========================================

* Region
tab region, m
tab region, nolabel
drop if region == -9
drop if region==19 //drop Northern Ireland
drop if region==17 //drop Wales
drop if region==18 //drop Scotland


* ========================================
* = Recoding of variables   =
* ========================================

* Time variable + 1997-dummy

tab time,m
*drop year
gen year = time + 1990
tab year

* ========================================
** 1) Political Ideology

tab opsoca 
tab opsocb 
tab opsocc 
tab opsocd 
tab opsoce 
tab opsocf



local waves = " a c"
foreach i of local waves {
gen opsoc_`i'_rev = opsoc`i'
recode opsoc_`i'_rev (-9/0=.) (5=1) (4=2) (2=4) (1=5)
}

local waves = " b d e f"
foreach i of local waves {
gen opsoc_`i'_rev = opsoc`i'
recode opsoc_`i'_rev (-9/0=.) 
}

egen econideo_s = rowmean(opsoc_a_rev  opsoc_c_rev opsoc_b_rev opsoc_d_rev  opsoc_e_rev  opsoc_f_rev)
gen econideo_scale = econideo_s *6
tab econideo_scale 

recode econideo_scale (6/13=1 "Left-wing") (13.1/18=0 "Centrist")(18.1/30=2 "Right-wing"), gen(econideo_cat)
tab econideo_cat

factor opsoc_a_rev -opsoc_f_rev, pcf mineigen(1.04)
predict factor_econ
corr factor_econ econideo_scale
*kdensity econideo_scale 

* ========================================
*** Catgorise Econ ideology for latent class model 

*lab def soclbl 1"leftist opinion" 3"right opinion"
/*
local waves = " b d e f"
foreach i of local waves {
gen econval_`i' = opsoc`i'
recode econval_`i' (-9/0=.) (2=1) (3=2) (4/5=3)
lab val  econval_`i' soclbl
tab  econval_`i'
}


* Reverse scale for item a and c
 
local waves = "a  c "
foreach i of local waves {
gen econval_`i' = opsoc`i'
recode econval_`i' (-9/0=.)(1=3) (2=3)(3=2) (4=1) (5=1)
lab val  econval_`i' soclbl
tab  econval_`i'
}
*/


*** Create smaller dataset ***

bys pid: egen validresp_econ = count(econideo_scale)
tab validresp_econ

drop if econideo_scale==. | validresp_econ==0

tab year,m
tab time,m
recode time (1=0) (3=1) (5=2) (7=3) (10=4) (14=5) (17=6)
tab time

* ========================================
** Party support
* ========================================


tab vote1 
tab vote2
tab vote3 
tab vote4
 
tab vote6
tab vote7 
tab vote8

* ========================================
* Party ID: Dummy
tab1 vote1 vote2
tab1 vote1 vote2, nolabel

*drop pid_dum
gen pid_dum =.
replace pid_dum = 1 if vote1==1 | vote2==1
replace pid_dum = 0 if vote1==2 & vote2==2

tab pid_dum vote2

lab def pid_dumlbl 1"Party ID" 0"No PID"
lab val pid_dum pid_dumlbl

* ========================================
*Party support

tab1 vote3 vote4
tab1 vote3 vote4, nolabel

*drop partyid
gen partyid =.
replace partyid = 1 if vote3==1 | vote4==1
replace partyid = 2 if vote3==2 | vote4==2
replace partyid = 3 if vote3==3 | vote4==3
replace partyid = 4 if (vote3>3 & vote3<10) |  (vote4>3 & vote4<10) 
replace partyid = 5 if vote3==10 | vote4==10
replace partyid = . if vote3>10 | vote4>10

tab vote3 partyid
tab vote4 partyid

lab def partyid 1 "cons" 2 "labour" 3 "libdem" 4 "other" 5"none"
lab val partyid partyid


* ========================================
* = Fewer partyid cats=
* ========================================


tab partyid vote1, col

tab partyid,m
tab partyid, nolabel

gen partyid2=partyid
recode partyid2 (4=0) (5=0)
lab def pidlbl1 0"No/other PID" 1"Conservatives" 2"Labour" 3"Libdem"
lab val partyid2 pidlbl1
tab partyid2

gen pid3cat = partyid2 

recode pid3cat (3=0)
lab val pid3cat pidlbl1
tab pid3cat, gen(partyID)

***------------
** Orginal Party ID

* Labour
gen pid_lab1 = 0 if econideo_scale!=.
replace pid_lab1 = 1 if pid3cat==2 & (year==1991 | year==1993 ) &econideo_scale!=.

sort pid year
browse pid year pid3cat pid_lab

by pid: egen pid_lab = max(pid_lab1)

* Conservatives
gen pid_con1 = 0 if econideo_scale!=.
replace pid_con1 = 1 if pid3cat==1 & (year==1991 | year==1993 ) &econideo_scale!=.

by pid: egen pid_con = max(pid_con1)

gen pid_time = pid_con
replace pid_time = 2 if pid_lab==1

lab var pid_time "Partisanship in wave 1 and 2"
lab val pid_time pidlbl1
tab pid_time

drop pid_lab1 pid_lab pid_con1 pid_con

** First three waves
* Labour
gen pid_lab1 = 0 if econideo_scale!=.
replace pid_lab1 = 1 if pid3cat==2 & (year==1991 | year==1993 | year==1995 ) &econideo_scale!=.

sort pid year
browse pid year pid3cat pid_lab

by pid: egen pid_lab = max(pid_lab1)

* Conservatives
gen pid_con1 = 0 if econideo_scale!=.
replace pid_con1 = 1 if pid3cat==1 & (year==1991 | year==1993 | year==1995 ) &econideo_scale!=.

by pid: egen pid_con = max(pid_con1)

gen pid_time3 = pid_con
replace pid_time3 = 2 if pid_lab==1

lab var pid_time3 "Partisanship in wave 1-3"
lab val pid_time3 pidlbl1
tab pid_time pid_time3

tab pid_time3 if econideo_scale!=.

** Party ID strength

tab vote5
codebook vote5
recode vote5 (-9/-1=.) (1=3 "very strong") (2=2 "fairly strong") (3=1 "not very strong"), gen(pid_strength)
bys pid3cat: sum pid_strength
sum pid_strength,d

bysort pid: egen mean_pidStrength = mean(pid_strength) if pid3cat!=.
lab var mean_pidStrength "Mean PID strength"
sum mean_pidStrength,d

gen mean_pidStrong = 0
replace mean_pidStrong=1 if mean_pidStrength>1.34
replace mean_pidStrong=. if pid3cat==.
lab var mean_pidStrong "Stong partisans across panel"

tab mean_pidStrong if year==1997
tab pid3cat mean_pidStrong if year==1997, row

* ========================================
** Political Interest

tab vote6,m
tab vote6, nolabel

gen polint = vote6
recode polint (-9/0=.) (1=4) (2=3) (3=2) (4=1)
lab def intlbl 1"not at all int" 2"not very int" 3"fairly int" 4"very int"
lab val polint intlbl
lab var polint "RECODE Political Interest"
tab polint


bysort pid: egen mean_int = mean(polint) if pid3cat!=.
lab var mean_int "Mean Interest"

gen mean_int2 =  mean_int* mean_int

corr mean_int partyID1 partyID2 partyID3 if year<1997

sum mean_int,d
gen mean_Strongint = 0
replace mean_Strongint=1 if mean_int>2.4
replace mean_Strongint=. if mean_int==.
lab var mean_Strongint "Stong pol interest across panel"
bys mean_Strongint: sum polint

tab mean_Strongint if year==1997
tab pid3cat mean_Strongint if year==1997, row

tab pid3cat mean_Strongint if year==1997  & mean_pidStrong==1


tab polint pid_strength if  pid3cat==2 & year==1991 
tab pid3cat mean_pidStrong if year==1991, row


* ========================================
** Partisanship and political interest 

gen partisan_polint = 0
replace partisan_polint = 1 if pid_time==1 & mean_Strongint==0
replace partisan_polint = 2 if pid_time==1 & mean_Strongint==1
replace partisan_polint = 3 if pid_time==2 & mean_Strongint==0
replace partisan_polint = 4 if pid_time==2 & mean_Strongint==1

lab def part_int 1 "Cons + no pol int" 2 "Cons + pol int" 3 "Lab + no pol int" 4 "Lab + pol int"
lab val partisan_polint part_int
lab var partisan_polint "Cat: Partisan + pol interest"
tab partisan_polint if econideo_scale!=.

tab partisan_polint pid_time

* ========================================
** First year included in study

egen min_year = min(year) if econideo_scale!=., by(pid)
*browse pid year min_year partyid2 
tab min_year



* ========================================
** 1a) Age: Min

bysort pid: gen age1 = age if year== min_year
recode age1 (.=-2)
bysort pid: egen min_age = max(age1)
lab var min_age "Age at t=0"
recode min_age (-2=.)
drop age1

sum min_age,d

drop if age>65


* ========================================
** Gender
recode sex (-9 -7 = .) (1=0) (2=1), gen(female)
drop sex
* ========================================
** 3) Region: min

tab region, m
tab region, nolabel

bysort pid: gen reg1 = region if year== min_year 
recode reg1 (.=-2)
bysort pid: egen min_region = max(reg1)
lab var min_region "Region at t=0"
recode min_region (-2=.)
drop reg1

recode min_region (1 / 2 = 1) (13/14 = 12) (15=16) (7=8) (9/10=11)

lab def reglbl 3 "r. of south east" 4 " south west " 5 " east anglia " 6 "east midlands"  ///
8 "r. of west midlands" 11 " r. of north west" 12 "yorkshire & humber"  ///
16"r. of north east " 1 "Greater London"

lab val min_region reglbl
tab min_region

*browse pid year min_year partyid2 region min_region 


* ========================================
** 4a) Social Class: Min

** Recode Goldthorpe 
tab jbgold if jbgold>0
tab jbgold, nolabel

recode jbgold (-9=.)(-8=0 "None") (1/2=1 "Service") (3=2 "Intermediate") (4=3 "Lower sales services") (5/7=4 "Self-employed") (8=5 "Technicians") (9/11=6 "Manual workers"), gen(class)

tab jbgold class
tab class

*xtreg esec4, i(pid) mle

****************

bysort pid: gen cla1 = class if year== min_year 
recode cla1 (.=-2)
bysort pid: egen min_class6 = max(cla1)
lab var min_class6 "Class (6 Cat) at t=0"
recode min_class6 (-2=.)
drop cla1

rename min_class6 min_class
lab def classlbl 2"Intermediate" 4"Self-employed" 3"Lower sales services" 5"Technicians" 6"Manual workers" 1 "Service" 0 "None"
lab val min_class classlbl

tab min_class class

tab min_class

*browse pid year min_year partyid2 esec4 min_class 



* ========================================
** 5a) Housing: Min

recode tenure (-9=.) (1 2=1 own) (3 4=2 scoial) (5/8=3 rented), gen(housing)
recode tenure (-9=.) (1=1 owned) (2=2 mortgage) (3 4=3 scoial) (5/8=4 rented), gen(housing2)
tab housing



*xtreg housing2, i(pid) mle

*xtmixed housing2 time || pid:, mle

***************************

bysort pid: gen tem1 = housing2  if year== min_year 
recode tem1 (.=-2)
bysort pid: egen min_housing = max(tem1)
lab var min_housing  "Housing at t=0"
recode min_housing (-2=.)
drop tem1

*browse pid year min_year partyid2 housing2 min_housing

lab def houslbl 2"Mortgage" 3"Social" 4"Rented" 1"Own"
lab val min_housing houslbl
tab min_housing



* ========================================
** 6a) Education: Min

*recode qfedhi (-9 -7 =.) (1 2=1 degree) (3/11=2 intermediate) (12 13=3 noqual), g(educ)


tab casmin, m
tab casmin, nolabel
recode casmin (-7/-1=.)
tab casmin

tab isced
tab isced, nolabel
recode isced (-7/0=.)

tab qfedhi
tab qfedhi, nolabel

capture drop educ
gen educ = isced
recode educ  (7=6)  (2=1) 
replace educ=1 if qfedhi==13
lab def educlbl 1 "Primary or none" 3 "low sec-voc" 4 "hisec-mivoc" 5"higher voc" 6 "degree"
lab val educ educlbl
tab educ
 
*************************

bysort pid: gen tem1 = educ  if year== min_year 
recode tem1 (.=-2)
bysort pid: egen min_educ = max(tem1)
lab var min_educ  "Education at t=0"
recode min_educ (-2=.)
drop tem1

lab val min_educ educlbl
tab min_educ

recode min_educ (0=.)
** 5b) Education: Max

bysort pid: egen max_educ = max(educ) if pid3cat!=.
lab var max_educ "Max Education"


lab val max_educ educlbl
tab max_educ
recode max_educ (0=.)




* ========================================
* INDEPENDENT VARIABLES: MATERIAL INTEREST
* ========================================

* Set panel structure
tsset pid time

* ============================================
**  Employment 

tab1 jbft jbstat
tab jbstat, nolabel

***** Drop retired people and those in full-time eductaion

drop if  jbstat==4 | jbstat==6


**** Gen unemploymenet
tsset pid time
gen jbstat1= l.jbstat

gen empl=0
replace empl = 1 if (jbstat==1 | jbstat==2) & (jbstat1==1 | jbstat1==2)
replace empl=. if jbstat==.
tab empl

gen unemp =0
replace unemp = 1 if jbstat==3

tsset pid time
gen unemp_1 = l.unemp

*drop unemp_ch

gen unemp_ch = .
replace unemp_ch=0 if unemp_1!=.
replace unemp_ch = 1 if unemp==1 & unemp_1==1
replace unemp_ch = 2 if unemp==1 & unemp_1==0
replace unemp_ch = 3 if unemp==0 & unemp_1==1
lab def unemplbl1 0"NO unemp in T and T-1" 1"Unemp in T and T-1" 2 "Became unemp in T" 3 "Found job in T"
lab val unemp_ch unemplbl1

tab unemp_ch 


* ========================================
** 7a) Income: Min

rename fiyr incyear
kdensity incyear if incyear <100000

gen inc_log = log(incyear)
kdensity inc_log

*************************

bysort pid: gen tem1 = inc_log  if year== min_year 
recode tem1 (.=-2)
bysort pid: egen min_inc = max(tem1)
lab var min_inc  "Income at t=0"
recode min_inc (-2=.)
drop tem1

sum min_inc,d

xtile min_inc5=min_inc, nq(5)
tab min_inc5

xtile income5=inc_log, nq(5)
tab income5

*** Logged change in income ****

tsset pid time
gen inc_1 = l.incyear

*drop inc_ch
gen inc_ch = incyear / inc_1 if emp==1
kdensity inc_ch if inc_ch<3

sum inc_ch,d

capture drop inc_drop
gen inc_drop = 0 if inc_1!=.
replace inc_drop = 1 if inc_ch<0.751
replace inc_drop = 2 if inc_ch>1.25
replace inc_drop = . if empl!=1

lab def inclbl 0 "No significant changes" 1"Drop of income by at last 25%" 2 "Increase of income by at last 25%"
lab val inc_drop inclbl
tab inc_drop


*** ATTITUDE: Change in financial situation

tab1 fisit fisitc fisitx
tab1 fisit fisitc fisitx, nolabel

gen fisit_n = fisit
gen fisitc_n = fisitc
gen fisitx_n = fisitx

recode fisit (-9/-1=.) 
recode fisitc (-9/-1=.)  
recode fisitx (-9/-1=.) 

recode fisit_n (-9/-1=.) (1/2=1) (3=0)(4/5=-1)
recode fisitc_n (-9/-1=.) (3=0) (1=1) (2=-1)
recode fisitx_n (-9/-1=.) (3=0) (1=1) (2=-1)

lab def fisitlbl -1"Fin.sit bad/got worse" 0 "Same/comfortable" 1"Fin.sit better/well"
lab val fisit_n fisitc_n fisitx_n  fisitlbl

* Current financial situation

tsset pid time
gen fin_1 = l.fisit_n

gen finsit_drop = .
replace finsit_drop=0 if fin_1==fisit_n & emp==1
replace finsit_drop=1 if (fin_1==0 & fisit_n==-1) | (fin_1==1 & fisit_n==0) & emp==1
replace finsit_drop=2 if (fin_1==-1 & fisit_n==0) | (fin_1==0 & fisit_n==1) & emp==1
lab def flbl1 0"Fin sit same" 1 "Fin sit got worse" 2 "Fin sit got better" 
lab val finsit_drop flbl1
tab finsit_drop


*** ATTITUDE: Job security

tab jbsat4
tab jbsat4, nolabel

gen jobsec = jbsat4
recode jobsec (-9/0=.)
tab jobsec


gen jobsec_cat =.
replace jobsec_cat =0 if jobsec>3
replace jobsec_cat = 1 if jobsec<4

lab def joblbl 1"Unsecure Jobsec" 0"Secure job"
lab val jobsec_cat joblbl
tab jobsec_cat 

tsset pid time
gen job_1 = l.jobsec_cat

gen jobsec_drop = 0 if job_1!=.
replace jobsec_drop=1 if job_1==0 & jobsec_cat==1 & emp==1
replace jobsec_drop=2 if job_1==1 & jobsec_cat==0 & emp==1
replace jobsec_drop=. if empl!=1

lab def jslbl 0"Job sec did not get worse" 1 "Job sec got worse" 2"Job sec got better"
lab val jobsec_drop jslbl
tab jobsec_drop



*Explorative 

tab1 jobsec_drop finsit_drop inc_drop unemp_ch 
corr jobsec_drop finsit_drop inc_drop unemp_ch 

* Generate dummies
tab jobsec_drop, gen(jobsec_drop_) 
tab finsit_drop, gen(finsit_drop_) 
tab inc_drop, gen(inc_drop_)
tab unemp_ch, gen(unemp_ch_) 


**** Safe DATA *****
tab time
recode time (0=.)

saveold "Data/BHPS_atleast1validresponses", replace


drop if econideo_scale==. | validresp_econ<3
saveold "Data/BHPS_atleast3validresponses", replace


***Prepare for sub-grpup analysis - Party id and pol. interest
**  -----------------------------------------
** Partisnaship in 1997
use "Data/BHPS_atleast3validresponses.dta" , clear

** Labour
gen pid_lab_all1 = 0 if econideo_scale!=.
replace pid_lab_all1 = 1 if (pid3cat==2 & year==1997) & econideo_scale!=.
tab pid_lab_all1

by pid: egen pid_lab_97 = sum(pid_lab_all1)
tab pid_lab_97

** Tories
gen pid_con_all1 = 0 if econideo_scale!=.
replace pid_con_all1 = 1 if (pid3cat==1 & year==1997) & econideo_scale!=.
by pid: egen pid_con_97 = sum(pid_con_all1)
tab pid_con_97

** Indep
gen pid_ind_all1 = 0 if econideo_scale!=.
replace pid_ind_all1 = 1 if (pid3cat==0 & year==1997) & econideo_scale!=.
by pid: egen pid_ind_97 = sum(pid_ind_all1)

gen pid3cat_97 = 0 if pid_ind_97==1
replace pid3cat_97 = 1 if pid_con_97==1
replace pid3cat_97 = 2 if pid_lab_97==1
lab val pid3cat_97 pidlbl1

tab pid3cat_97 pid3cat if year==1997

drop pid_lab_97 pid_lab_all pid_con_97 pid_con_all1

*browse pid year pid3cat pid_lab pid_lab_all1 pid_lab_97

saveold "Data/BHPS_PID97.dta", replace version(12) 

**  -----------------------------------------
** Partisnaship in first three waves
use "Data/BHPS_atleast3validresponses.dta" , clear

** Labour
capture drop pid_lab_all
gen pid_lab_all1 = 0 if econideo_scale!=.
replace pid_lab_all1 = 1 if pid3cat==2 & (year==1991 | year==1993 | year==1995 ) & econideo_scale!=.
tab pid_lab_all1

by pid: egen pid_lab_all = sum(pid_lab_all1)
tab pid_lab_all

browse pid year pid3cat pid_lab pid_lab_all1 pid_lab_all
drop if pid_lab_all<3

tab pid_lab_all if year==1997
tab pid3cat

saveold "Data/BHPS_Lab3.dta", replace version(12) 

**  -----------------------------------------
** Partisnaship in 1997
use "Data/BHPS_atleast3validresponses.dta" , clear

** Labour
capture drop pid_lab_all
gen pid_lab_all1 = 0 if econideo_scale!=.
replace pid_lab_all1 = 1 if (pid3cat==2 & year==1997) & econideo_scale!=.
tab pid_lab_all1

by pid: egen pid_lab_97 = sum(pid_lab_all1)
tab pid_lab_97

browse pid year pid3cat pid_lab pid_lab_all1 pid_lab_97
drop if pid_lab_97!=1


saveold "Data/BHPS_Lab97.dta", replace version(12) 



** Labour + political interest 
use "Data/BHPS_atleast3validresponses.dta" , clear

capture drop pid_lab pid_lab1

gen pid_lab1 = 0 if econideo_scale!=.
replace pid_lab1 = 1 if pid3cat==2 & (year==1991 | year==1993 ) &econideo_scale!=.

sort pid year
browse pid year pid3cat pid_lab

by pid: egen pid_lab = max(pid_lab1)

drop if pid_lab!=1
keep if mean_Strongint==1

saveold "Data/BHPS_Lab_polint.dta", replace version(12) 
